/**
 * 1. 没有借助对象池，如果内容比较多建议借助对象池，提前创建
 * 2. 无法做重复利用
 * 3. 一次性创建所有内容，没有根据显示个数做处理
 * 4. 不支持删除操作
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class CgwScrollView extends cc.Component {

    @property(cc.Prefab)
    prefab: cc.Prefab = null;
    @property({
        type: cc.Component.EventHandler,
        displayName: "内容处理函数"
    })
    delegate = new cc.Component.EventHandler();

    //item数量
    @property
    totalCount: number = 0;
    //间隔

    @property
    num: number = 1

    @property
    cellWidth: number = 100

    @property
    cellHeight: number = 100;

    private scrollView: cc.ScrollView;
    private content: cc.Node;
    private contents: cc.Node[] = []
    private itemSize: number = 0;
    start() {
        this.scrollView = this.getComponent(cc.ScrollView)
        if (this.scrollView && this.prefab) {
            this.content = this.scrollView.content;
            // this.scrollView.node.on('scrolling', this.updateItem, this);
            this.init();
        }
    }

    //这里可以通过对象池获取
    getItem() {
        return cc.instantiate(this.prefab)
    }
    init() {
        let temp = this.getItem()
        let count = temp.childrenCount;
        let cCount = this.content.childrenCount
        if (cCount != count) {
            console.error(' layout的个数 更item 直接子节点的个数不匹配 ', cCount, count)
        }

        for (let index = 0; index < cCount; index++) {
            this.contents[index] = this.content.children[index];
        }
        // let layout = this.contents[0].getComponent(cc.Layout)
        this.itemSize = 0;
        let bufferSize = 0;
        if (this.scrollView.vertical) {
            this.itemSize = (this.cellHeight);
            bufferSize = this.scrollView.node.height;
        } else {
            this.itemSize = (this.cellWidth);
            bufferSize = this.scrollView.node.width;
        }

        let size = Math.floor(this.totalCount / this.num)
        size = this.totalCount % this.num == 0 ? size : size + 1;

        cc.log(' size == ', size)

        let contentSize = size * this.itemSize
        if (this.scrollView.vertical) {
            this.content.height = contentSize;
            for (let index = 0; index < this.contents.length; index++) {
                const element = this.contents[index];
                element.height = contentSize;
            }
        } else {
            this.content.width = contentSize;
            for (let index = 0; index < this.contents.length; index++) {
                const element = this.contents[index];
                element.width = contentSize;
            }
        }



        //cc.log(' content count ', this.contents.length)
        for (let index = 0; index < this.totalCount; index++) {
            let item: cc.Node = this.getItem()
            if (!item) {
                continue;
            }

            let count = item.childrenCount

            //cc.log(' count == ', count)

            if (count > 0) {
                let j = 0;
                let list = item.children;
                while (list.length > 0) {
                    // cc.log(' list.length  11111 ', list.length)
                    let element = list[0]
                    if (element) {
                        element.parent = null;
                        //cc.log(' list.length 2222222 ', list.length)
                        this.contents[j].addChild(element)
                        j++;
                    } else {
                        cc.warn(' element is null ', j)
                        break;
                    }
                }
            } else {

            }


            this.delegate.emit([this, index, item]);
        }
        // this.content.width = this.contents[0].width;
        cc.log('  this.content.height ', this.content.height)
        cc.log('  this.content.width ', this.content.width)
        // this.content.height = this.contents[0].height;
    }

    // update (dt) {}
}
